import {combineReducers} from 'redux-immutable';
import Immutable from 'immutable';
import * as actions from './actions';
import {getPathById} from './utils';
import {
    GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN,
    GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS,
    GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL
} from 'Shared/actions/currentUserContext';
import {DEFAULT_QUERY_OPTION} from './constants';
const EMPTY_ARRAY = [];
const initData = (state, action) => {
    switch(action.type) {
        case actions.GET_INIT_BEGIN:
            return state.merge({
                isFetching: true,
                message: ''
            });

        case actions.GET_INIT_SUCCESS:
            return state.merge({
                isFetching: false,
                isDealerUser: action.data.isDealerUser,
                brands: action.data.brands || EMPTY_ARRAY,
                dealers: action.data.dealers || EMPTY_ARRAY,
                message: ''
            });

        case actions.GET_INIT_FAIL:
            return state.merge({
                message: action.message,
                isFetching: false
            });

        default:
            return state;
    }
};

const list = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                total: 0,
            });
        case actions.GET_LIST_SUCCESS:
            return state.merge({
                isFetching: false,
                total: action.data.totalElements,
                message: '',
                data: action.data.content
            });
        case actions.GET_LIST_FAIL:
            return state.merge({
                isFetching: false,
                total: 0,
                message: action.message,
                data: []
            });
        default:
            return state;
    }
};

const permission = (state, action) => {
    switch(action.type) {
        case GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN:
            return state.merge(Immutable.fromJS({
                isFetching: true,
                message: ''
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: '',
                data: action.data
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: action.message
            }));
        default:
            return state;
    }
};

const detail = (state, action) => {
    switch(action.type) {
        case actions.GET_DETAIL_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                data: {}
            });
        case actions.GET_DETAIL_SUCCESS:
            return state.merge({
                isFetching: false,
                message: '',
                data: action.data
            });
        case actions.GET_DETAIL_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message,
                data: {}
            });
        default:
            return state;
    }
};

const dealers = (state, action) => {
    switch(action.type) {
        case actions.GET_DEALERS_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                total: 0,
            });
        case actions.GET_DEALERS_SUCCESS:
            return state.merge({
                isFetching: false,
                total: action.data.totalElements,
                message: '',
                data: action.data.content
            });
        case actions.GET_DEALERS_FAIL:
            return state.merge({
                isFetching: false,
                total: 0,
                message: action.message || '',
                data: []
            });
        case actions.CLEAR_DEALER:
            return state.merge({
                isFetching: false,
                total: 0,
                message: action.message || '',
                data: []
            });
        default:
            return state;
    }
};

const marketDepartments = (state, action) => {
    switch(action.type) {
        case actions.GET_MARKETDEPARTMENTS_TOP_SUCCESS:
            return state.set('data', Immutable.fromJS(action.data.content || EMPTY_ARRAY));
        case actions.GET_MARKETDEPARTMENTS_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
            });
        case actions.GET_MARKETDEPARTMENTS_SUCCESS: {
            if(!action.name)
                return state.merge({
                    isFetching: false,
                    message: '',
                    data: action.data
                });
            const path = getPathById(state.get('data').toJS(), action.name);
            return state.withMutations(map => map.set('isFetching', false).setIn(['data', ...path, 'items'], Immutable.fromJS(action.data)));
        }
        default:
            return state;
    }
};

const queryCondition = (state, action) => {
    switch(action.type) {
        case actions.CHANGE_DEALER:
            return state.merge(action.data);
        case actions.SAVE_QUERY_CONDITION:
            return state.set(action.name, action.value);
        case actions.RESET_QUERY_PANEL:
            return Immutable.fromJS(DEFAULT_QUERY_OPTION);
        case actions.GET_LIST_SUCCESS:
            return state.merge(action.condition);
        default:
            return state;
    }
};

const queryBySort = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_SUCCESS:
            return state.merge(action.condition);
        default:
            return state;
    }
};

const locatedKey = (state, action) => {
    switch(action.type) {
        case actions.SET_LOCATE:
            return action.key;
        case actions.CLEAR_DEALER:
            return '';
        default:
            return state;
    }
};


const queryConditionForDealer = (state, action) => {
    switch(action.type) {
        case actions.CLEAR_DEALER:
            return state.merge({
                code: '',
                name: '',
                shortCode: '',
                shortName: '',
                marketDepartmentId: '',
            });
        case actions.SAVE_DEALER_CONDITION:
            return state.set(action.name, action.value);
        case actions.GET_DEALERS_SUCCESS:
            return state.withMutations(map => map
                .set('pageSize', action.condition.pageSize)
                .set('sortField', action.condition.sortField)
                .set('isDesc', action.condition.isDesc)
            );
        default:
            return state;
    }
};

const queryBySortForDealer = (state, action) => {
    switch(action.type) {
        case actions.CLEAR_DEALER:
            return state.merge({
                code: '',
                name: '',
                shortCode: '',
                shortName: '',
                marketDepartmentId: '',
            });
        case actions.GET_DEALERS_SUCCESS:
            return state.merge(action.condition);
        default:
            return state;
    }
};


const domainData = combineReducers({
    list,
    permission,
    detail,
    initData,
    dealers,
    marketDepartments,
});
const appState = combineReducers({
    queryCondition,
    queryBySort,
    locatedKey,
    queryConditionForDealer,
    queryBySortForDealer

});
export default combineReducers({
    domainData,
    appState,
});
